package com.middle.experiment01.Controller;

import com.middle.experiment01.Result.PageResult;
import com.middle.experiment01.Result.Result;
import com.middle.experiment01.Service.CourseService;
import com.middle.experiment01.entity.Course;
import com.middle.experiment01.entity.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/course/common")
@Slf4j
public class CourseController {
    @Autowired
    private CourseService courseService;


    @PostMapping
    public Result save(@RequestBody Course course){
        log.info("添加课程信息：{}",course);
        courseService.save(course);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable String id){
        log.info("删除课程功能接口");
        courseService.deleteById(id);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Course course){
        log.info("更新课程:{}的信息",course.getId());
        courseService.update(course);
        return Result.success();
    }

    @GetMapping("/{id}")
    //根据课程id查询课程信息
    public Result<Course> getById(@PathVariable String id){
        log.info("当前查询的id:{}",id);
        Course course = courseService.getById(id);
        return Result.success(course);
    }

    @GetMapping("/page")
    public Result<PageResult> pageAllCourse(){
        log.info("查询所有的课程");
        List<Course> courses=courseService.pageAllCourse();
        return Result.success(new PageResult(courses.size(),courses));
    }

    @GetMapping("/getstudent/{id}")
    public Result<List<Student>> getStudentById(@PathVariable String id){
        log.info("查询当前课程下的所有学生");
        List<Student> list=courseService.getStudentById(id);
        return Result.success(list);
    }

    //TODO 和上面 多个学生可选多个课程，和前面实现原理一样 只需插入一个中间表即可...
}
